package com.hotline.monitor.schedule;


import com.hotline.monitor.constant.Constants;
import com.hotline.monitor.dto.AgentStatusDTO;
import com.hotline.monitor.dto.SkillWaitNumRequestDTO;
import com.hotline.monitor.service.agent.AgentEventService;
import com.hotline.monitor.service.agent.AgentGroupService;
import com.hotline.monitor.service.agent.OnlineAgentService;
import com.hotline.monitor.service.monitor.MonitorService;
import com.hotline.monitor.service.quality.QualityControlService;
import com.hotline.monitor.service.skill.QueueDeviceService;
import com.hotline.monitor.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.springframework.util.StringUtils.replace;

@Configuration
@EnableScheduling
@Slf4j
public class MonitorSchedule {

    @Autowired
    private QueueDeviceService queueDeviceService;

    @Autowired
    private AgentGroupService agentGroupService;

    @Autowired
    private MonitorService monitorService;

    @Autowired
    private AgentEventService agentEventService;

    @Autowired
    private QualityControlService qualityControlService;


    @Value("${org.id}")
    private String orgIds;

    @Value("${agent.worknumber}")
    private String workNumber;

    /**
     * 定时设置坐席状态，每天7点到20点，一秒执行一次
     */
    @Scheduled(cron = "${agent.status.cron}")
    private void task() {
        try{
            //设置坐席状态
            List<AgentStatusDTO> agentStatusList = agentGroupService.onlineAgentOnVdn();
            if(StringUtils.isNotBlank(orgIds)){
                orgIds = replace(orgIds,"，",",");
                List<String> orgIdList = StringUtils.splitListByString(orgIds);
                for(String orgId : orgIdList){
                    monitorService.setAgentStatus(agentStatusList,orgId);
                }
            }
            //技能排队数据
            List<SkillWaitNumRequestDTO> skillWaitNumList =  queueDeviceService.getQueueDeviceQueryAcdStat();
            monitorService.setOrgWaitNum(skillWaitNumList);

            //12366技能排队数据
            List<SkillWaitNumRequestDTO> skillWaitNum12366List =  queueDeviceService.getQueueDeviceQueryAcdStat12366();
            monitorService.setOrgWaitNum12366(skillWaitNum12366List);

            //12345技能排队数据
            List<SkillWaitNumRequestDTO> skillWaitNum12345List =  queueDeviceService.getQueueDeviceQueryAcdStat12345();
            monitorService.setOrgWaitNum12345(skillWaitNum12345List);

            //技能队列排队数
            //monitorService.setQueueSkillWaitNum(skillWaitNumList);

            //超时坐席强制签出
            //timeoutAgentForceLogin();
        }catch (Exception e){
            log.error("定时设置坐席状态出错：{}",e.getMessage());
        }
    }



    /**
     * 超时坐席强制签出
     */
    private void timeoutAgentForceLogin(){
        List<String> agentList = agentEventService.getTimeoutAgentList();
        if(!CollectionUtils.isEmpty(agentList)){
            Map header = new HashMap();
            header.put("Guid", Constants.HEADER_GUID);
            for(String workNo : agentList){
                qualityControlService.forceLogout(workNumber,workNo,header);
            }
        }
    }
}
